package mainWindowSupport;

/*
 *  ____  _____ ____  ____  _____ ____ ___    _  _____ _____ ____  
 * |  _ \| ____|  _ \|  _ \| ____/ ___|_ _|  / \|_   _| ____|  _ \ 
 * | | | |  _| | |_) | |_) |  _|| |    | |  / _ \ | | |  _| | | | |
 * | |_| | |___|  __/|  _ <| |__| |___ | | / ___ \| | | |___| |_| |
 * |____/|_____|_|   |_| \_\_____\____|___/_/   \_\_| |_____|____/ 
 * 
 */

import java.awt.Image;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.*;

import actionHandlers.*;
import palette.CurrentColor;

//singleton
public class ToolBar extends JToolBar{
	private static final long serialVersionUID = -6992081273357506612L;
	private static ToolBar inst;
	
	public static ToolBar getInst() {
		if (inst==null) {
			inst = new ToolBar();
		}
		return inst;
	}
	
	/***********************************************
	 *  Constructor: makes, and places each button
	 **********************************************/
	JButton arrowButton;
	JButton boxButton;
	JButton ellipseButton;
	JButton colorButton;
	JButton undoButton;
	JButton redoButton;
	protected ToolBar(){
		
		//Add default buttons
		arrowButton = new JButton("Arrow");
		addIcon(arrowButton,"arrow.gif");
		this.add(arrowButton);
		boxButton = new JButton("Box");
		addIcon(boxButton,"box.gif");
		this.add(boxButton);
		ellipseButton = new JButton("Ellipse");
		addIcon(ellipseButton, "circle.gif");
		this.add(ellipseButton);
		colorButton = new JButton("        ");
		this.add(colorButton);
		updateColor();
		undoButton = new JButton("Undo");
		this.add(undoButton);
		redoButton = new JButton("Redo");
		this.add(redoButton);
		
		this.setRollover(true);

	    
	    //Register a listener for the buttons.
	    boxButton.addActionListener(new ShapeSelector());
	    arrowButton.addActionListener(new ShapeSelector());
	    ellipseButton.addActionListener(new ShapeSelector());
	    colorButton.addActionListener(new ColorHandler());
	}
	
	public void updateColor() {
		colorButton.setBackground(CurrentColor.getInstance());
	}
	
	public void addIcon(JButton button, String imgName) {
		
		Image img = null;
		String imgLocation = "images/" + imgName;
		try {
			img = ImageIO.read(new File(imgLocation));
		} catch (IOException e) {
			System.out.println("Resource not found: " + imgLocation);
		}
		img = img.getScaledInstance(10, 10, 0);
		
		if (img != null)
			button.setIcon(new ImageIcon(img, button.getText()));
	    
	}
}
